<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="900px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="分配类型" prop="assignmentType">
        <el-radio-group v-model="formData.assignmentType">
          <el-radio :value="1">考评员分配</el-radio>
          <el-radio :value="2">监考员分配</el-radio>
          <el-radio :value="3">考场分配</el-radio>
        </el-radio-group>
      </el-form-item>
      
      <el-form-item label="评价计划" prop="planId">
        <el-select v-model="formData.planId" placeholder="请选择评价计划">
          <el-option label="2023年第四季度电工评价" value="1" />
          <el-option label="2023年第四季度焊工评价" value="2" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="评价科目" prop="subjectId">
        <el-select v-model="formData.subjectId" placeholder="请选择评价科目">
          <el-option label="理论考试" value="1" />
          <el-option label="实操考试" value="2" />
          <el-option label="综合面试" value="3" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="考试日期" prop="examDate">
        <el-date-picker
          v-model="formData.examDate"
          type="date"
          placeholder="选择考试日期"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      
      <el-form-item label="考试时间" prop="examTime">
        <el-time-picker
          v-model="formData.examTime"
          placeholder="选择考试时间"
          value-format="HH:mm:ss"
        />
      </el-form-item>
      
      <el-form-item label="考场名称" prop="examRoom">
        <el-input v-model="formData.examRoom" placeholder="请输入考场名称" />
      </el-form-item>
      
      <!-- 人员选择 -->
      <el-form-item label="分配人员" prop="assignedPersonnel">
        <el-transfer
          v-model="formData.assignedPersonnel"
          :data="availablePersonnel"
          :titles="['可选人员', '已选人员']"
          :props="{ key: 'id', label: 'name' }"
          filterable
          filter-placeholder="搜索人员"
        />
      </el-form-item>
      
      <el-form-item label="分配备注" prop="assignmentNote">
        <el-input
          v-model="formData.assignmentNote"
          type="textarea"
          :rows="3"
          placeholder="请输入分配备注"
        />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <el-button @click="submitForm" type="primary" :loading="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'

const emit = defineEmits(['success'])
const dialogVisible = ref(false)
const dialogTitle = computed(() => (formData.value.id ? '修改人员分配' : '添加人员分配'))
const formLoading = ref(false)
const formRef = ref()
const formData = ref({
  id: undefined,
  assignmentType: 1,
  planId: '',
  subjectId: '',
  examDate: '',
  examTime: '',
  examRoom: '',
  assignedPersonnel: [] as string[],
  assignmentNote: ''
})
const formRules = ref({
  assignmentType: [{ required: true, message: '分配类型不能为空', trigger: 'change' }],
  planId: [{ required: true, message: '评价计划不能为空', trigger: 'change' }],
  subjectId: [{ required: true, message: '评价科目不能为空', trigger: 'change' }],
  examDate: [{ required: true, message: '考试日期不能为空', trigger: 'change' }],
  examTime: [{ required: true, message: '考试时间不能为空', trigger: 'change' }],
  assignedPersonnel: [{ required: true, message: '请至少选择一个人员', trigger: 'change' }]
})

// 可选人员列表
const availablePersonnel = ref([
  { id: '1', name: '张三 - 高级考评员' },
  { id: '2', name: '李四 - 中级考评员' },
  { id: '3', name: '王五 - 监考员' },
  { id: '4', name: '赵六 - 考评员' },
  { id: '5', name: '钱七 - 高级监考员' }
])

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  resetForm()
  if (id) {
    formLoading.value = true
    try {
      // TODO: 加载分配详情
      formData.value.id = id
    } finally {
      formLoading.value = false
    }
  }
}

/** 提交表单 */
const submitForm = async () => {
  const elForm = formRef.value
  if (!elForm) return
  const valid = await elForm.validate()
  if (!valid) return
  formLoading.value = true
  try {
    const data = formData.value
    if (data.id) {
      // TODO: 修改人员分配
    } else {
      // TODO: 新增人员分配
    }
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    assignmentType: 1,
    planId: '',
    subjectId: '',
    examDate: '',
    examTime: '',
    examRoom: '',
    assignedPersonnel: [],
    assignmentNote: ''
  }
  if (formRef.value) {
    formRef.value.clearValidate()
  }
}

defineExpose({ open })
</script> 